/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package src;


import java.awt.Color;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import static src.General.dateFormater;

/**
 *Για validation JTextField με SimpleDateFormat
 * 
 */
public class SimpleDateFormatInputVerifier extends InputVerifier{
    private final JLabel validationLabel;
  
    public SimpleDateFormatInputVerifier(JLabel validationLabel) {
        super();
        this.validationLabel = validationLabel;
    }
    
    @Override
    public boolean verify(JComponent input) {
        boolean result = true;
        try {
            String noWhiteSpaces = ((JTextField)input).getText().replaceAll("\\s",General.Empty);
            if (noWhiteSpaces.length() > 0)
                ((JTextField)input).setText(dateFormater.format(dateFormater.parse(noWhiteSpaces)));
            else 
                ((JTextField)input).setText(General.Empty);
            if (!General.Empty.equals(noWhiteSpaces) && dateFormater.parse(((JTextField)input).getText()).compareTo(dateFormater.parse("31/12/9999"))> 0) 
                result = false;
                        
        } catch (Exception ex) {
            result = false;
        }
        if (!result)
            this.validationLabel.setForeground(Color.red);
        else
            this.validationLabel.setForeground(Color.black);
        return result;
    }

}
